package com.example.demo;

import com.alibaba.fastjson.JSONObject;
import org.apache.http.*;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;

@SpringBootApplication
public class Demo1Application {

    public static void main(String[] args) {
        SpringApplication.run(Demo1Application.class, args);
        String key="531f7eda35d265a43c7091a5971802aab320a80c16afb6e7164c595a";
        String name="index_basic";
        String s="{\"api_name\": \"stock_basic\", \"token\": \"531f7eda35d265a43c7091a5971802aab320a80c16afb6e7164c595a\", \"params\": {\"list_stauts\":\"L\"}, \"fields\": \"ts_code,name,area,industry,list_date\"}";
        String canshu="{\\\"api_name\\\": \\\""+name+"\\\", \\\"token\\\": \\\""+key+"\\\", \\\"params\\\": {\\\"market\\\":\\\"SW\\\"}, \\\"fields\\\": \\\"ts_code,name,market\\\"}";

        try{
            String s1 = doPosts("http://api.waditu.com",canshu);
//
            net.sf.json.JSONObject jsonObject = net.sf.json.JSONObject.fromObject(s1.toString());
//
          System.out.println(s1);
           // System.out.println(deviceRequest());
        }catch (Exception e){

        }
    }

    /**
     * java发送raw
     * @author zengwei
     * @email 1014483974@qq.com
     * @version 2019年3月01日 下午4:02:02
     */
    public static JSONObject deviceRequest() {

        JSONObject result = null;

        try {
            String url = "http://api.waditu.com";
            JSONObject json = new JSONObject();
           // json.put("{\"api_name\": \"stock_basic\", \"token\": \"531f7eda35d265a43c7091a5971802aab320a80c16afb6e7164c595a\", \"params\": {\"list_stauts\":\"L\"}, \"fields\": \"ts_code,name,area,industry,list_date\"}" );
            json.put("api_name","stock_basic");
            json.put("token","531f7eda35d265a43c7091a5971802aab320a80c16afb6e7164c595a");

            JSONObject json2 = new JSONObject();
            json2.put("list_stauts","L");
            json.put("params", json2);
            json.put("fields","ts_code,name,area,industry,list_date");
            System.out.println(json);
            @SuppressWarnings({"resource"})
            HttpClient httpClient = new DefaultHttpClient();
            HttpPost post = new HttpPost(url);
            StringEntity postingString = new StringEntity(json.toJSONString());
            post.setEntity(postingString);
            post.setHeader("Content-type", "application/json");
            HttpResponse response = httpClient.execute(post);
            String content = EntityUtils.toString(response.getEntity());

            result = (JSONObject) JSONObject.parse(content);

            System.out.println(result);

        } catch (ParseException | IOException e) {
            e.printStackTrace();
        }

        return result;
    }



/*    public static String sendHttpPost(String url, String JSONBody) throws Exception {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(url);
        httpPost.addHeader("Content-Type", "application/json");
        httpPost.setEntity(new StringEntity(JSONBody));
        CloseableHttpResponse response = httpClient.execute(httpPost);
//		System.out.println(response.getStatusLine().getStatusCode() + "\n");
        HttpEntity entity = response.getEntity();
        String responseContent = EntityUtils.toString(entity, "UTF-8");
//		System.out.println(responseContent);
        response.close();
        httpClient.close();
        return responseContent;
    }*/

    private static Logger logger = Logger.getLogger(HttpUtil.class);

    public static String doPosts(String url, Object object) throws IOException {
        System.out.println("Start ....................>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
        CloseableHttpClient httpclient = HttpClients.createDefault();

        HttpPost httpPost = new HttpPost(url);// 创建httpPost

        httpPost.setHeader("Accept", "application/json");

        httpPost.setHeader("Content-Type", "application/json");

        StringEntity entity = new StringEntity(JSONObject.toJSONString(object), "utf-8");

        System.out.println(JSONObject.toJSONString(object));

        httpPost.setEntity(entity);

        CloseableHttpResponse response = null;

        try {
            response = httpclient.execute(httpPost);

            StatusLine status = response.getStatusLine();

            int state = status.getStatusCode();

            if (state == HttpStatus.SC_OK) {
                HttpEntity responseEntity = response.getEntity();

                return EntityUtils.toString(responseEntity);

            } else {
                logger.error("请求返回:" + state + "(" + url + ")");

            }

        } finally {
            if (response != null) {
                try {
                    response.close();

                } catch (IOException e) {
                    e.printStackTrace();

                }

            }

            try {
                httpclient.close();

            } catch (IOException e) {
                e.printStackTrace();

            }

        }

        return null;

    }
}
